#ifndef _DISPATCH_TASK_SERVICE_H_
#define _DISPATCH_TASK_SERVICE_H_

#include "bkshandler.h"
#include "bksevent.h"
#include "logger.h"
#include "global_def.h"
#include "thread_pool.h"
#include "networkinterface.h"
#include "bike.pb.h"
#include "eventtype.h"
#include "event_def.h"

#include <mutex>
#include <atomic>
#include <map>
#include <algorithm>
#include <queue>

using namespace tutorial;

class DispatchTaskService{
public:
    DispatchTaskService()
    {
        _stop.store(FALSE);
    }
    virtual ~DispatchTaskService()
    {

    }

    void subscribe(u32 eid, BKSHandler *handler);
    void unsubscribe(u32 eid, BKSHandler *handler);

    BOOL open();
    void close();

    i32 enqueue(BKSEvent *ev);
    static void service(void *arg);
    std::vector<BKSEvent*> process(BKSEvent *ev);

    BKSEvent* praseToReqEvent(char *_read_msg_buf, u32 read_msg_len, void *arg);

    static DispatchTaskService* getInstance();

private:
    static DispatchTaskService *_DTS;
    static std::mutex m;
    typedef std::vector<BKSHandler*> _handlers;
    typedef std::map<u32, _handlers> Ev_handlers_Map;
    Ev_handlers_Map _ev_handlers_Map;
    std::atomic_bool _stop;
    thread_pool_t *_pool;
    
public:
    static std::queue<BKSEvent*> _rsp_evs_queue;
};

#endif